package com.hoshiicloud.saleorder.controller.customer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.saleorder.param.customer.CartAddParam;
import com.hoshiicloud.saleorder.param.customer.CartDeleteParam;
import com.hoshiicloud.saleorder.param.customer.CartQueryParam;
import com.hoshiicloud.saleorder.param.customer.CartUpdateParam;
import com.hoshiicloud.saleorder.service.customer.ICartService;
import com.hoshiicloud.user.entity.customer.Cart;
import com.hoshiicloud.user.vo.customer.CartDetailVO;
import com.hoshiicloud.user.vo.customer.CartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 购物车购物信息记录表 前端控制器
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-28
 */
@Api(value = "前台用户操作", tags = {"购物车购物信息记录表管理"})
@Slf4j
@Validated
@RestController
@RequestMapping("/customer/cart")
public class CartController {

    @Autowired
    private ICartService cartService;

    @ApiOperation(value = "新增购物车购物信息记录表", notes = "")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Response add(@RequestBody @Valid CartAddParam param) {
        log.info("新增参数:" + param.toString());
        Cart entity = new Cart();
        BeanUtils.copyProperties(param, entity);
        // TODO 只接受指定参数
        return cartService.insert(entity);
    }

    @ApiOperation(value = "修改购物车购物信息记录表", notes = "")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Response update(@RequestBody @Valid CartUpdateParam param) {
        Cart entity = new Cart();
        BeanUtils.copyProperties(param, entity);
        // TODO 只接受指定参数
        return cartService.updateDataById(entity);
    }

    @ApiOperation(value = "更新购物车商品失效状态", notes = "检查购物车商品是否失效")
    @RequestMapping(value = "/checkCart", method = RequestMethod.POST)
    public Response updateCartStatus(@RequestParam String userId) {
        if (StringUtils.isBlank(userId)) {
            return Response.failed("用户id不能为空");
        }

        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Cart::getUserId, userId)
                .eq(Cart::getStatus, 1)
                .eq(Cart::getDeleted, 0);
        return cartService.updateCartInfo(queryWrapper);
    }

    @ApiOperation(value = "根据ID和数量删除购物车购物信息记录表", notes = "")
    @RequestMapping(value = "/deleteByNumber", method = RequestMethod.POST)
    public Response deleteByNumber(@RequestBody CartDeleteParam param) {
        //TODO 参数检查
        return cartService.deleteDataByNumber(param);
    }

    @ApiOperation(value = "删除购物车购物信息记录表", notes = "")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response delete(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            return Response.failed("id不能为空");
        }
        return cartService.deleteDataById(id);
    }

    @ApiOperation(value = "批量删除购物车购物信息记录表", notes = "")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    public Response deleteByIds(@RequestBody List<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            ids.sort(null);
            return cartService.deleteDataByIds(ids);
        } else {
            return Response.failed("列表不能为空");
        }
    }


    @ApiOperation(value = "获取购物车购物信息记录表详情", notes = "")
    @RequestMapping(value = "/getDetail", method = RequestMethod.GET)
    public Response<CartDetailVO> getDetail(@RequestParam String id) {
        if (StringUtils.isBlank(id)) {
            return Response.failed("id不能为空");
        }
        return cartService.getDetail(id);
    }

    @ApiOperation(value = "获取购物车购物信息记录表列表", notes = "")
    @RequestMapping(value = "getList", method = RequestMethod.POST)
    public Response<List<CartVO>> getList(@RequestBody @Valid CartQueryParam param) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .in(param.getRecId() != null && param.getRecId().size() > 0, Cart::getRecId, param.getRecId())
                .eq(param.getUserId() != null, Cart::getUserId, param.getUserId())
                .eq(param.getStatus() != null, Cart::getStatus, param.getStatus())
                .eq(param.getDeleted() != null, Cart::getDeleted, param.getDeleted());
        String key = "CartQueryParam{" + param.toString() + "}, Page{pageNumber=" + param.getPageNumber()
                + ", pageSize=" + param.getPageSize() + "}";
        return cartService.getList(queryWrapper, key);
    }

    @ApiOperation(value = "分页查询购物车购物信息记录表", notes = "")
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    public Response<IPage<CartVO>> getPage(@RequestBody @Valid CartQueryParam param) {

        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(param.getStoreId() != null, Cart::getStoreId, param.getStoreId())
                .eq(param.getPlatformId() != null, Cart::getPlatformId, param.getPlatformId())
                .eq(param.getUserId() != null, Cart::getUserId, param.getUserId())
                .eq(param.getStatus() != null, Cart::getStatus, param.getStatus())
                .eq(param.getDeleted() != null, Cart::getDeleted, param.getDeleted());
        IPage<Cart> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
        String key = "CartQueryParamToPage{" + param.toString() + "}, Page{pageNumber=" + param.getPageNumber()
                + ", pageSize=" + param.getPageSize() + "}";
        return cartService.getPage(page, queryWrapper, key);
    }


    @ApiOperation(value = "查询用户购物车数量")
    @RequestMapping(value = "/getCount", method = RequestMethod.POST)
    public Response<Integer> getCount(@RequestBody @Valid CartQueryParam param) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(param.getUserId() != null, Cart::getUserId, param.getUserId())
                .eq(param.getStatus() != null, Cart::getStatus, param.getStatus());
        return Response.data(cartService.getCount(queryWrapper));
    }

    /**
     * 根据用户ID和店铺ID批量删除
     *
     * @param userId 用户ID
     * @param storeId 店铺ID
     * @return Response
     * @author yangcheng
     */
    @ApiOperation(value = "根据用户ID和店铺ID批量删除", notes = "根据用户ID和店铺ID批量删除购物车购物信息记录表", consumes = MediaType.ALL_VALUE)
    @DeleteMapping(value = "/deleteByUserIdAndStoreId")
    public Response deleteByUserIdAndStoreId(@RequestParam("userId") Long userId,
            @RequestParam("storeId") Long storeId) {
        if (null != storeId && null != userId) {
            return cartService.deleteByUserIdAndStoreId(userId, storeId);
        } else {
            return Response.failed("用户ID和店铺ID不能为空");
        }
    }

    // public static void main(String[] arags) {
    //     CartQueryParam param = new CartQueryParam();
    //     param.setPlatformId(3189609977215975481L);
    //     param.setStoreId(74L);
    //     System.out.println(param.toString());
    // }
}
